<?php

namespace addons\member\model;


class MnemonicWords extends \web\common\model\BaseModel {

    protected function _initialize() {
        $this->tableName = 'mnemonic_words';
    }

    public function setMnemonicWords($code){
        $data['words_md5'] = md5(implode(',', $code->words));
        $data['mnemonic_words'] = implode(',', $code->words);
        $data['private_key'] = $code->entropy;
        $data['create_time'] = NOW_DATETIME;
        $data['update_time'] = NOW_DATETIME;
        return $this->add($data);
    }
    
    /**
     * 是否使用过该助记词
     */
    public function isUsed($mnemonic_words){
        $private_key = $this->where(['mnemonic_words'=>$mnemonic_words,'status'=>0])->value('private_key');
        if($private_key){
            return $private_key;
        }else{
            return false;
        }
    }


    /**
     * 使用过该助记词
     */
    public function setUsed($mnemonic_words,$private_key){
        $res = $this->where(['mnemonic_words'=>$mnemonic_words,'private_key'=>$private_key,'status'=>0])->update(['status'=>1]);
        if($res){
            return true;
        }else{
            return false;
        }
    }
}
